#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：123.py 
@File ：批量打包备份数据.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2025/1/3 下午2:20 
"""

import os
import shutil
import zipfile
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

# 配置字典，假设字典的内容如下：
config_dict = {
	'2sz': '../unit_2sz_new',
	'3abc': '/path/to/data/3abc',
	'4xyz': '/path/to/data/4xyz'
}


def find_and_process_folders(base_path, folder_names):
	folders_to_zip = []

	for folder_name in folder_names:
		backup_folder = os.path.join(base_path, 'backup')
		if not os.path.exists(backup_folder):
			continue

		num_folder = None
		letter_folder = None

		for folder in os.listdir(backup_folder):
			if folder_name in folder:
				if folder.isdigit():
					num_folder = folder
				elif any(char.isalpha() for char in folder):
					letter_folder = folder

		if num_folder and letter_folder:
			num_folder_path = os.path.join(backup_folder, num_folder)
			letter_folder_path = os.path.join(backup_folder, letter_folder)

			target_path = os.path.join(letter_folder_path, num_folder)
			if not os.path.exists(target_path):
				shutil.copytree(num_folder_path, target_path)
				folders_to_zip.append(letter_folder_path)
			else:
				print(f"Target folder {target_path} already exists. Skipping copy.")
				folders_to_zip.append(letter_folder_path)

	return folders_to_zip


def create_zip(zip_name, folders):
	with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
		for folder in folders:
			for root, _, files in os.walk(folder):
				for file in files:
					file_path = os.path.join(root, file)
					zipf.write(file_path, os.path.relpath(file_path, folder))
	print(f"Created zip: {zip_name}")


def on_process_click():
	selected_option = combobox.get()  # 获取选择的下拉框选项
	input_folders = entry.get().split(',')  # 获取输入的文件夹名，按逗号分隔

	if not input_folders:
		messagebox.showerror("输入错误", "请输入文件夹名称")
		return

	base_path = config_dict.get(selected_option)
	if not base_path:
		messagebox.showerror("路径错误", "未找到对应的路径")
		return

	individual_zips = []
	for folder_name in input_folders:
		folder_name = folder_name.strip()
		folders_to_zip = find_and_process_folders(base_path, [folder_name])

		if folders_to_zip:
			zip_name = f"{folder_name}.zip"
			create_zip(zip_name, folders_to_zip)
			individual_zips.append(zip_name)  # 保存每个单独的zip文件名

	# # 创建一个整体的压缩包，包含所有单独的压缩包
	# if individual_zips:
	# 	all_zips_path = os.path.join(base_path, 'all_zips')  # 存放所有单独压缩包的文件夹
	# 	# 删除该文件夹
	# 	if os.path.exists(all_zips_path):
	# 		shutil.rmtree(all_zips_path)
	# 	if not os.path.exists(all_zips_path):
	# 		os.makedirs(all_zips_path)
	# 	for zip_file in individual_zips:
	# 		shutil.copy(zip_file, all_zips_path)  # 复制单独压缩包到一个文件夹
	# 	create_zip(f"{selected_option}_all.zip",
	# 			   [os.path.join(all_zips_path, zip_file) for zip_file in individual_zips])

	messagebox.showinfo("处理完成", "文件夹打包完成")


# 创建主窗口
root = tk.Tk()
root.title("文件夹打包工具")

# 下拉框选择配置
tk.Label(root, text="选择配置：").grid(row=0, column=0, padx=10, pady=10)
combobox = ttk.Combobox(root, values=list(config_dict.keys()))
combobox.set("2sz")  # 默认选择
combobox.grid(row=0, column=1, padx=10, pady=10)

# 输入框
tk.Label(root, text="输入文件夹名称：").grid(row=1, column=0, padx=10, pady=10)
entry = tk.Entry(root, width=30)
entry.grid(row=1, column=1, padx=10, pady=10)

# 处理按钮
process_button = tk.Button(root, text="开始处理", command=on_process_click)
process_button.grid(row=2, column=0, columnspan=2, pady=20)

root.mainloop()
